package com.zmn.plat.admin.controller.channel.commission.settle.rule;

import com.zmn.common.constant.StatusConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.plat.model.entity.channel.commission.settle.rule.ChannelCommissionSettleRule;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 渠道佣金结算规则修改控制器
 *
 * @author linfeng
 *
 * @since 2020/10/26 10:31
 */
@RestController
@RequestMapping("/channel/commission/settle/rule/")
public class ChannelCommissionSettleRuleModifyController  extends ChannelCommissionSettleRuleBaseController  {


    /**
     * 新增修改
     * @author linfeng
     * @param channelCommissionSettleRule
     * @since 2020/10/26 10:31
     */
    @RequestMapping(value = "addEdit")
    public ResponseDTO addEdit(HttpServletRequest request, @RequestBody ChannelCommissionSettleRule channelCommissionSettleRule) {
        Date now = DateUtil.getNow();
        String opertor = getMcStaffName(request);

        Integer relateId = channelCommissionSettleRule.getRelateId();

        try {
            if (relateId == null ||relateId== 0) {
                channelCommissionSettleRule.setCreater(opertor);
                channelCommissionSettleRule.setCreateTime(now);
                channelCommissionSettleRule.setUpdater(opertor);
                channelCommissionSettleRule.setUpdateTime(now);
                channelCommissionSettleRuleService.insert(channelCommissionSettleRule);
            } else {
                channelCommissionSettleRule.setUpdater(opertor);
                channelCommissionSettleRule.setUpdateTime(now);
                channelCommissionSettleRuleService.updateByKey(channelCommissionSettleRule);
            }

            return ResponseDTO.success(StatusConsts.STATUS_SUCCESS, "操作成功！");
        } catch (Exception ex) {
            logger.error("[zmn]新增/修改渠道佣金结算规则失败,channelCommissionSettleRule:{}", channelCommissionSettleRule, ex);
            return ResponseDTO.fail("操作失败！");
        }
    }
}
